﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using ECG.Common;

namespace ECG.DAL
{
    public partial class NewsDb
    {
        public static List<NewsListHomeDto> GetAllNewsHomePaging(int page, int itemsPerPage, ref int totalPage)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Home_News_GetAll_Paging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                SqlParameter itemTotalpage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalpage.Direction = ParameterDirection.Output;
                var result = dao.GetDTOList<NewsListHomeDto>(ref command);
                totalPage = int.Parse((itemTotalpage.Value.ToString() == "" ? "0" : itemTotalpage.Value.ToString()));
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<NewsListHomeDto> GetAllNewsCatePaging(int page, int itemsPerPage, ref int totalPage, int cate)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Home_NewsCate_GetAll_Paging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                command.Parameters.Add(dao.CreateParameter("@NewsCategory", cate));
                SqlParameter itemTotalpage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalpage.Direction = ParameterDirection.Output;
                var result = dao.GetDTOList<NewsListHomeDto>(ref command);
                totalPage = int.Parse((itemTotalpage.Value.ToString() == "" ? "0" : itemTotalpage.Value.ToString()));
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<NewsListHomeDto> GetAllNewsTagPaging(int page, int itemsPerPage, ref int totalPage, string tagId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Home_NewsTag_GetAll_Paging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                command.Parameters.Add(dao.CreateParameter("@TagId", tagId));
                SqlParameter itemTotalpage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalpage.Direction = ParameterDirection.Output;
                var result = dao.GetDTOList<NewsListHomeDto>(ref command);
                totalPage = int.Parse((itemTotalpage.Value.ToString() == "" ? "0" : itemTotalpage.Value.ToString()));
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }
        public static List<NewsListOldDto> GetAllNewsNewTop(string newsId, string typ)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Home_News_Get10NewPost");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@NewsId", newsId));
                command.Parameters.Add(dao.CreateParameter("@Type", typ));
                var result = dao.GetDTOList<NewsListOldDto>(ref command);
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<NewsListOldDto> GetAllNewsOldTop(string newsId, string typ)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Home_News_Get10OldPost");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@NewsId", newsId));
                command.Parameters.Add(dao.CreateParameter("@Type", typ));
                var result = dao.GetDTOList<NewsListOldDto>(ref command);
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
